package chapter19.file;

import org.junit.jupiter.api.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @Auther: Tx
 * @Date: 2022/7/19 21:17
 * 演示FileInputStream的使用
 */
public class fileInputStream {
    public static void main(String[] args) {

    }
//读取文件，一个字节一个字节读
    @Test
    public void readFile01(){
        String filepath = "e://hello.txt";
        int Data;
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(filepath);
            while ((Data=fileInputStream.read())!=-1){
                System.out.print((char)Data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                fileInputStream.close();//关闭流
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void readFile02(){
        String filepath = "e://hello.txt";
        byte[] readData= new byte[8];
        int readLen = 0;
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(filepath);
            while ((readLen=fileInputStream.read(readData))!=-1){
                System.out.print(new String(readData,0,readLen));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                fileInputStream.close();//关闭流
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
